//
//  CYCollectView.m
//  CreditAddressBook
//
//  Created by Lee on 15/7/1.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYCollectView.h"
#import "CYStatistics.h"

@interface CYCollectView ()
@property (weak, nonatomic) IBOutlet UIImageView *levelImageView;//等级
@property (weak, nonatomic) IBOutlet UILabel *totalDurationLabel;//通话时长
@property (weak, nonatomic) IBOutlet UILabel *timesLabel;//通话次数
@property (weak, nonatomic) IBOutlet UILabel *callOutDurationLabel;//打出时长
@property (weak, nonatomic) IBOutlet UILabel *callOutTimesLabel;//打出次数
@property (weak, nonatomic) IBOutlet UILabel *averageCallTimesLabel;//平均通话次数
@property (weak, nonatomic) IBOutlet UILabel *averageCallDurationLabel;//平均每次通话时长

@end

@implementation CYCollectView

- (void)setStatistics:(CYStatistics *)statistics {
    _statistics = statistics;
    self.totalDurationLabel.text = [NSString stringWithFormat:@"%@", statistics.callRecord.callDurationSum];
    self.timesLabel.text = [NSString stringWithFormat:@"%@次", statistics.callRecord.times];
    self.callOutDurationLabel.text = [NSString stringWithFormat:@"%@", statistics.callOutDuration];
    self.callOutTimesLabel.text = [NSString stringWithFormat:@"%ld次", (long)statistics.callOutTimes];
    self.averageCallTimesLabel.text = [NSString stringWithFormat:@"平均每%.1f小时通话一次", statistics.averageCallTimes];
    self.averageCallDurationLabel.text = [NSString stringWithFormat:@"%@", statistics.averageCallDuration];
    
    [self setupLevel];
}

+ (instancetype)collectView {
    return [[[NSBundle mainBundle] loadNibNamed:@"CYCollectView" owner:nil options:nil] lastObject];
}

- (void)setupLevel {
    CGFloat y = 3 * log(self.statistics.callRecord.times.integerValue/100.f + 1);
    NSInteger i = y + 1;
//    CYLog(@"y:%ld", (long)i);
//    CYLog(@"y:%f", y);
    if (i > 8) {
        i = 8;
    }
    [self.levelImageView setImage:[UIImage imageWithName:[NSString stringWithFormat:@"icon_level%ld", (long)i]]];
}

- (void)awakeFromNib {
    self.backgroundColor = [UIColor clearColor];
    // 不需要跟随父控件的尺寸变化而伸缩
    self.autoresizingMask = UIViewAutoresizingNone;
}

- (void)setFrame:(CGRect)frame {
    frame.size.height = 677;
    [super setFrame:frame];
}

- (void)dealloc {
    CYLog(@"统计总汇销毁");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
